Micron Document
🎖️GitЯра🎖️

Node / meshcore / MeshCore / files / src / Utils.h

Displaying Raw • Download

src/Utils.h 93b2db63de9009af047a2d0e1bcd97b4b0ba7106 (93b2db63) Text, 3.44 KB

Tff7b72#Tff7b72pragma once

Tff7b72#Tff7b72include T8b949e<MeshCore.h>
Tff7b72#Tff7b72include T8b949e<Stream.h>
Tff7b72#Tff7b72include T8b949e<string.h>

Te6edf3namespace Te6edf3mesh Tb4b4b4{

Te6edf3class Te6edf3RNG Tb4b4b4{
Te6edf3publicTff7b72:
Te6edf3virtual Tffa657void Te6edf3randomTb4b4b4(Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tffa657size_t Te6edf3szTb4b4b4) Tff7b72= T79c0ff0Tb4b4b4;

T8b949e/**
* \returns random number between _min (inclusive) and _max (exclusive)
*/
Tffa657uint32_t Td2a8ffnextIntTb4b4b4(Tffa657uint32_t Te6edf3_minTb4b4b4, Tffa657uint32_t Te6edf3_maxTb4b4b4)Tb4b4b4;
Tb4b4b4}Tb4b4b4;

Te6edf3class Te6edf3Utils Tb4b4b4{
Te6edf3publicTff7b72:
T8b949e/**
* \brief calculates the SHA256 hash of 'msg', storing in 'hash' and truncating the hash to 'hash_len' bytes.
*/
Tff7b72static Tffa657void Te6edf3sha256Tb4b4b4(Tffa657uint8_t Tff7b72*Te6edf3hashTb4b4b4, Tffa657size_t Te6edf3hash_lenTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3msgTb4b4b4, Tffa657int Te6edf3msg_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief calculates the SHA256 hash of two fragments, 'frag1' and 'frag2' (in that order), storing in 'hash' and truncating.
*/
Tff7b72static Tffa657void Td2a8ffsha256Tb4b4b4(Tffa657uint8_t Tff7b72*Te6edf3hashTb4b4b4, Tffa657size_t Te6edf3hash_lenTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3frag1Tb4b4b4, Tffa657int Te6edf3frag1_lenTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3frag2Tb4b4b4, Tffa657int Te6edf3frag2_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief Encrypts the 'src' bytes using AES128 cipher, using 'shared_secret' as key, with key length fixed at CIPHER_KEY_SIZE.
* Final block is padded with zero bytes before encrypt. Result stored in 'dest'.
* \returns The length in bytes put into 'dest'. (rounded up to block size)
*/
Tff7b72static Tffa657int Td2a8ffencryptTb4b4b4(Tff7b72const Tffa657uint8_tTff7b72* Te6edf3shared_secretTb4b4b4, Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657int Te6edf3src_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief Decrypt the 'src' bytes using AES128 cipher, using 'shared_secret' as key, with key length fixed at CIPHER_KEY_SIZE.
* 'src_len' should be multiple of block size, as returned by 'encrypt()'.
* \returns The length in bytes put into 'dest'. (dest may contain trailing zero bytes in final block)
*/
Tff7b72static Tffa657int Td2a8ffdecryptTb4b4b4(Tff7b72const Tffa657uint8_tTff7b72* Te6edf3shared_secretTb4b4b4, Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657int Te6edf3src_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief encrypts bytes in src, then calculates MAC on ciphertext, inserting into leading bytes of 'dest'.
* \returns total length of bytes in 'dest' (MAC + ciphertext)
*/
Tff7b72static Tffa657int Td2a8ffencryptThenMACTb4b4b4(Tff7b72const Tffa657uint8_tTff7b72* Te6edf3shared_secretTb4b4b4, Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657int Te6edf3src_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief checks the MAC (in leading bytes of 'src'), then if valid, decrypts remaining bytes in src.
* \returns zero if MAC is invalid, otherwise the length of decrypted bytes in 'dest'
*/
Tff7b72static Tffa657int Td2a8ffMACThenDecryptTb4b4b4(Tff7b72const Tffa657uint8_tTff7b72* Te6edf3shared_secretTb4b4b4, Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657int Te6edf3src_lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief converts 'src' bytes with given length to Hex representation, and null terminates.
*/
Tff7b72static Tffa657void Td2a8fftoHexTb4b4b4(Tffa657charTff7b72* Te6edf3destTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657size_t Te6edf3lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief converts 'src_hex' hexadecimal string (should be null term) back to raw bytes, storing in 'dest'.
* \param dest_size must be exactly the expected size in bytes.
* \returns true if successful
*/
Tff7b72static Tffa657bool Td2a8fffromHexTb4b4b4(Tffa657uint8_tTff7b72* Te6edf3destTb4b4b4, Tffa657int Te6edf3dest_sizeTb4b4b4, Tff7b72const Tffa657char Tff7b72*Te6edf3src_hexTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief Prints the hexadecimal representation of 'src' bytes of given length, to Stream 's'.
*/
Tff7b72static Tffa657void Td2a8ffprintHexTb4b4b4(Te6edf3StreamTff7b72& Te6edf3sTb4b4b4, Tff7b72const Tffa657uint8_tTff7b72* Te6edf3srcTb4b4b4, Tffa657size_t Te6edf3lenTb4b4b4)Tb4b4b4;

T8b949e/**
* \brief parse 'text' into parts separated by 'separator' char.
* \param text the text to parse (note is MODIFIED!)
* \param parts destination array to store pointers to starts of parse parts
* \param max_num max elements to store in 'parts' array
* \param separator the separator character
* \returns the number of parts parsed (in 'parts')
*/
Tff7b72static Tffa657int Te6edf3parseTextPartsTb4b4b4(Tffa657charTff7b72* Te6edf3textTb4b4b4, Tff7b72const Tffa657charTff7b72* Te6edf3partsTb4b4b4[Tb4b4b4]Tb4b4b4, Tffa657int Te6edf3max_numTb4b4b4, Tffa657char Te6edf3separatorTff7b72=Ta5d6ff'Ta5d6ff,Ta5d6ff'Tb4b4b4)Tb4b4b4;

Tff7b72static Tffa657bool Td2a8ffisHexCharTb4b4b4(Tffa657char Te6edf3cTb4b4b4)Tb4b4b4;
Tb4b4b4}Tb4b4b4;

Tb4b4b4}

Served by rngit 1.5.2 - Generated in 0.04s